Page({
    data: {
        urlAddress: getApp().globalData.imageUrl,
        cartData:[],
        goodsType: '',
        selectAll:true
    },
    onLoad(option){
        this.setData({
            goodsType:option.type,
            cartPrice:option.cartPrice,
            cartDiscountAmount:option.cartDiscountAmount
        })
        console.log(option.type);
    },
    onShow(){
       this.getCar(1);
    },
    //获取购物车
    getCar(type){
        let _seft=this;
        wx.showLoading({
			title: '加载中',
			mask: true
		})
        wx.request({
			url: getApp().globalData.baseUrl+"/applet/reserve/cart/cart-settle.do",
			data: {
                shopCode: getApp().globalData.common.reserveShopCode,
					type: 2
            },
			header: getApp().globalData.header,
			success: function(res) {
                wx.hideLoading();
				if (res.data.code == 200) {
                    let obj=res.data.obj;
                    for(let i=0;i<obj.cartItems.length;i++){
                        obj.cartItems[i].goods.flag=true;
                    }
                    //console.log(obj)
                    _seft.setData({
                        cartData:obj,
                        number:obj.num
                    });
                    // console.log(res.data.obj.cartItems)
                }
        	}
		})
    },
    //添加购物车
    addAction: function(e) {
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})

		//请求加入购物车
		console.log(e.currentTarget.dataset.type.categoryCode);
		_self.addCarRequest(e.currentTarget.dataset.code,
			getApp().globalData.common.reserveShopCode,
			e.currentTarget.dataset.num,
			e.currentTarget.dataset.type, 1);
		//1 加入购物车 2默认加入购物车,3加入后页面不刷新
	},
    //请求加入购物车
	addCarRequest(gbk, shopCode, num, categoryCode, flag) {
		let _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/cart/add.do',
			data: {
				gbk: gbk,
				shopCode: shopCode,
				num: num,
				categoryCode: categoryCode,
				type: 2
			},
			header: getApp().globalData.header,
			success: function(res) {
                console.log(res);
                wx.hideLoading();
                if(flag==3){
                }else{
                    _self.getCar();
                }
                _self.setData({
                    cartPrice:res.data.obj.cartPrice,
                    cartDiscountAmount:res.data.obj.cartDiscountAmount,
                    number:res.data.obj.num
                })
			}
		})
    },
    turnPayPage: function() {
		getApp().globalData.common.reserveVid = null;
		wx.navigateTo({
			url: '../reservePay/reservePay?type=2'
		})
	},
    //单选
    bindSelect(e){
        var obj=this.data.cartData;
        var _self=this;
       //减去
       if(e.currentTarget.dataset.flag){
            for(let i=0;i<obj.cartItems.length;i++){
                if(obj.cartItems[i].goods.goodsGbk==e.currentTarget.dataset.code){
                    obj.cartItems[i].goods.flag=false;
                }
            }
            this.setData({
                selectAll:!this.data.selectAll,
            });
            //请求加入购物车
		_self.addCarRequest(e.currentTarget.dataset.code,
			getApp().globalData.common.reserveShopCode,
			-e.currentTarget.dataset.num,
			_self.data.goodsType, 3);
		//1 加入购物车 2默认加入购物车
        //添加
       }else{
        for(let i=0;i<obj.cartItems.length;i++){
            if(obj.cartItems[i].goods.goodsGbk==e.currentTarget.dataset.code){
                obj.cartItems[i].goods.flag=true;
            }
        }
       }
    },
        //全选
    bindSelectAll(){
        var obj=this.data.cartData;
        let _self = this;
        //取消全选
        if(this.data.selectAll){
          for(let i=0;i<obj.cartItems.length;i++){
            obj.cartItems[i].goods.flag=false;
        }
        wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/cart/clear.do',
			data: {
                shopCode: getApp().globalData.common.reserveShopCode,
                type: 2
			},
			header: getApp().globalData.header,
			success: function(res) {
                _self.setData({
                    selectAll:!_self.data.selectAll,
                    cartData:{},
                    cartPrice:0,
                 cartDiscountAmount:0
                });
                wx.switchTab({
                    url: '../reserve/reserve'
                })
			}
        })
        //全选
        }else{
            for(let i=0;i<obj.cartItems.length;i++){
                obj.cartItems[i].goods.flag=true;
            } 
	
        }

       
    },
})